package com.selenium;


import java.time.Duration;
import java.util.NoSuchElementException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Test1 {
	
	Logger log = LoggerFactory.getLogger(Test1.class);
	
    private static String driverPath = "C:/Users/domains90/AppData/Local/Google/Chrome/Application/chromedriver.exe";

	public static void main(String[] args) throws InterruptedException {
		Test1 t = new Test1();
		t.ticketGrabbing();
	}

    public void ticketGrabbing() throws InterruptedException{
        System.getProperties().setProperty("webdriver.chrome.driver", driverPath);
        ChromeOptions op = new ChromeOptions();
        op.addArguments("--remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(op);
        driver.manage().window().maximize();
        driver.get("https://www.damai.cn/");
    	try {    
            //切换到登入
            driver.findElement(By.className("login-user")).click();     
    		log.info("title:{}",driver.getTitle());

    		//等待10s 扫码登入
    		Thread.sleep(10000);
    		
    		driver.findElement(By.className("input-search")).sendKeys("时代少年团");;     
    		
		} finally{
			Thread.sleep(10000);
			driver.quit();
		}
      
    }
    
    public void han(WebDriver driver) {
        driver.manage().window().maximize();
        driver.get("https://www.damai.cn/");
        //当前窗口句柄
        String originalWindow = driver.getWindowHandle();
        log.info("当前句柄:{}",originalWindow);
        //切换到登入
        driver.findElement(By.className("login-user")).click();
        
        for (String windowHandle : driver.getWindowHandles()) {
        	System.out.println(windowHandle);
            if(!originalWindow.contentEquals(windowHandle)) {
                driver.switchTo().window(windowHandle);
                break;
            }
        }
    }
    
    public WebElement wait(WebDriver driver,By by) {
    	Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    			  .withTimeout(Duration.ofSeconds(30))
    			  .pollingEvery(Duration.ofSeconds(2))
    			  .ignoring(NoSuchElementException.class);
    	WebElement ele = wait.until(dr -> {
    		  return dr.findElement(by);
    	});
    	return ele;
    }
	
}
